/**
 * 入参
    type： 1- 指令   2-簿记
    onOk：指令下达成功或簿记成功抛出事件
    onCancel：取消抛出事件

*/
import React, { PropTypes, PureComponent, Fragment } from 'react';
import { PrimaryC, AvailableAssetC, TradeHoldC, FirstHeaderC, SecondHeaderC, PrimaryGhostC } from '../../../../comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col, Card, Tabs, Input, Spin, Button, message, Table, Divider, DatePicker, Select, Modal } from 'antd';
import style from './index.less';
import HKinfoC from './HKInformation';
import AddOrderC from './addOrder';

const confirm = Modal.confirm;
import { connect } from 'dva';
import 'moment/locale/zh-cn';
import moment from 'moment';
const { TabPane } = Tabs;
@Form.create()
@connect(({ HKstockM, userM: user, loading }) => ({
  HKstockM,
  user,
  loading,
}))
export default class AddHKStock extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      money: {}, //资金
      portfolio: undefined, //当前组合
      position: {}, //持仓数量
      tradingcode: undefined, //当前证券代码 {exchangecode: "101", tradingcode: "510060", secuabbr: "上证中央企业50ETF"}
    };
  }

  componentDidMount() {}

  UNSAFE_componentWillReceiveProps(nextProps) {}

  //组合变更id 切换
  portfolioOnChange = (e) => {
    if (this.state.portfolio !== e) {
      //组合id不同则变更当前页面组合id
      this.setState({
        portfolio: e,
      });
    }
  };
  //证券代码变更
  tradingcodeOnChange = (e) => {
    if (this.state.tradingcode !== e) {
      //证券代码不同则变更当前页面证券代码
      this.setState({
        tradingcode: e,
      });
    }
  };
  //新建指令和簿记成功之后调用
  onOk = () => {
    const { type } = this.props;
    if (type == 2 || type == 11) {
      //簿记页面
      this.props.onOk();
    }
  };
  //点击取消按钮
  onCancel = () => {
    const { type } = this.props;
    if (type == 2 || type == 11) {
      //簿记页面
      this.props.onCancel();
    }
  };
  render() {
    const { portfolio, tradingcode } = this.state;
    return (
      <div className={style.AddHKStock}>
        <div>
          <HKinfoC
            tradingcode={tradingcode && tradingcode.tradingCode ? tradingcode.tradingCode : undefined}
            exchangecode={tradingcode && tradingcode.exchangeCode ? tradingcode.exchangeCode : undefined}
            onChange={(info) => {
              this.setState({
                lotSize: info.lotSize,
                optionTickSize: info.optionTickSize,
                isStop: info.busStatus,
              });
            }}
          />
        </div>
        <div>
          <div style={{ marginBottom: '16px' }}>
            <AvailableAssetC
              portfolio={portfolio}
              hkType={2}
              onChange={(e) => {
                this.setState({
                  money: e,
                });
              }}
            />
          </div>
          <div style={{ marginBottom: '16px' }}>
            <TradeHoldC
              securityType={'1'} //港股通
              hkType={2}
              portfolio={portfolio}
              tradingcode={tradingcode && tradingcode.tradingCode ? tradingcode.tradingCode : undefined}
              exchangecode={tradingcode && tradingcode.exchangeCode ? tradingcode.exchangeCode : undefined}
              onChange={(e) => {
                this.setState({
                  position: { ...e },
                });
              }}
            />
          </div>
          <FirstHeaderC title={'交易要素'} />
          <AddOrderC
            type={this.props.type}
            portfolioOnChange={this.portfolioOnChange}
            tradingcodeOnChange={this.tradingcodeOnChange}
            onRef={(ref) => {
              this.addOrder = ref;
            }}
            onOk={this.onOk}
            onCancel={this.onCancel}
            money={this.state.money}
            position={this.state.position}
            lotSize={this.state.lotSize}
            optionTickSize={this.state.optionTickSize}
            isStop={this.state.isStop}
          />
        </div>
      </div>
    );
  }
}
